<?php
include_once '/var/www/html/amanager/db/db.function.php';

abstract class Mouvement {
    const Credit = 1;
    const Debit = 2;
    const Neutral = 0;
}

class ligneHistorique {
    public function __construct() {
        $this->loadFromCSV("CA20150302_2117.CSV");
    }
    
    public function save($hash,$date,$commentaire,$valeur,$type){
        $ecommentaire = addslashes($commentaire);
        
        $res = db_query("INSERT INTO historique (hash,date,commentaire,valeur,type)"
                . "     VALUES ("
                . "             '{$hash}',"
                . "             {$date},"
                . "             '{$ecommentaire}',"
                . "             {$valeur},"
                . "             {$type})");
        if($res && db_affected_rows()>0){
            return true;
        } else {
            echo db_last_error();
        }
    }
    
    public function loadFromCSV($filename){
        $countError = 0;
        $countCredit = 0;
        $credit_somme = 0;
        $countDebit = 0;
        $debit_somme = 0;
        
        $ajouts = array();
        
        if (($handle = fopen($filename, "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
                
                $date = isset($data[0])?str_replace("/", "-", $data[0]):null;
                $commentaire = isset($data[1])?$data[1]:'';
                $debit = isset($data[2])?$data[2]:'';
                $credit = isset($data[3])?$data[3]:'';
                
                if(isset($date) 
                   && strtotime($date)
                   && !empty($commentaire)){
                    
                    $type = Mouvement::Neutral;
                    $value = 0;
                    if(!empty($debit)) { //C'est un débit
                        $type = Mouvement::Debit;
                        $countDebit++;
                        $debit_somme += $debit;
                        $value = $debit;
                    } elseif(!empty($credit)) { //C'est un crédit
                        $type = Mouvement::Credit;
                        $countCredit++;
                        $credit_somme += $credit;
                        $value = $credit;
                    }
                    $ajouts[md5($date.$commentaire.$value)] = array(
                      'date' => strtotime($date),
                      'commentaire' => $commentaire,
                      'valeur' => str_replace(",",".", $value),
                      'type' => $type
                    );
                } elseif(isset($date)
                           && strtotime($date)) {
                    $countError++;
                }
            }
            fclose($handle);
            
            echo "Crédit ($countCredit) :$credit_somme<br>";
            echo "Débit ($countDebit) :$debit_somme<br>";
            echo "Total :".($countCredit+$countDebit);
            
            foreach($ajouts as $hash => $ajout){
                $this->save($hash,$ajout['date'],$ajout['commentaire'],$ajout['valeur'],$ajout['type']);
            }
        }
    }
}

$o = new ligneHistorique();